//
//  AppDelegate.m
//  ImportConfig
//
//  Created by syni on 2017/3/15.
//  Copyright © 2017年 syni 		BobBob. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application 启动程序
    [[_window standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES]; //隐藏最小化按钮
    [[_window standardWindowButton:NSWindowZoomButton] setHidden:YES]; //隐藏最大化按钮
    // [_window setDelegate:self];
    [_window setTitle:@"配置导入"];
    //[_window setStyleMask:NSBorderlessWindowMask]; // 不显示边框
    // [_window setFrame:NSMakeRect(450, 250, 400, 300) display:YES]; //设置窗口显示位置和大小
    [_window setFrame:NSMakeRect(450, 250, 450, 300) display:YES animate:YES];
    [NSApp activateIgnoringOtherApps:YES]; //窗口置顶
}


-(IBAction)btnPress:(id)sender
{
    if (sender == pBtnBrowser)
    {
        NSOpenPanel *pOpenPanel = [NSOpenPanel openPanel];
        // [pOpenPanel setTitle:@"导入配置文件"];//setTitle为NSWindow的方法,它是openPanel的父类
        [pOpenPanel setAllowsMultipleSelection:NO]; //禁用多选模式
        [pOpenPanel setCanChooseFiles:YES];
        [pOpenPanel setMessage:@"请选择Server导出的配置文件."];
        
        NSInteger iResult = [pOpenPanel runModal]; //显示OpenPanel
        if (iResult == NSFileHandlingPanelOKButton)
        {
            NSString *nstrFilePath = [[pOpenPanel URL] path];
            // NSLog(nstrFilePath);
            [pTfFilePath setStringValue:nstrFilePath];
        }
    }
    
    if (sender == pBtnOK)
    {
        NSString *filePath = [pTfFilePath stringValue]; // 获取文本框中的路径
        NSFileManager *fileManger = [NSFileManager defaultManager];
        if ([fileManger fileExistsAtPath:filePath])
        {
            NSError *error = nil;
            [[NSFileManager defaultManager]copyItemAtPath:filePath toPath:@"/opt/SDPConfig.json" error: &error];
            if (error != nil)
            {
                NSAlert *pAlert = [[NSAlert alloc] init];
                [pAlert setMessageText:@"警告"];
                [pAlert addButtonWithTitle:@"确定"];
                [pAlert setInformativeText:@"导入配置文件失败"];
                [pAlert setAlertStyle:NSWarningAlertStyle];
                [pAlert beginSheetModalForWindow:[self window] completionHandler:^(NSModalResponse returnCode) {
                    
                }];
            }
            else
            {
                NSAlert *pAlert = [[NSAlert alloc] init];
                [pAlert setMessageText:@"提示"];
                [pAlert addButtonWithTitle:@"确定"];
                [pAlert setInformativeText:@"导入成功"];
                [pAlert setAlertStyle:NSInformationalAlertStyle];
                [pAlert beginSheetModalForWindow:[self window] completionHandler:^(NSModalResponse returnCode) {
                    [NSApp terminate:self]; // 导入成功，退出程序
                }];
            }
        }
        else
        {
            NSAlert *pAlert = [[NSAlert alloc] init];
            [pAlert setMessageText:@"警告"];
            [pAlert setInformativeText:@"您还未选择要导入配置文件"];
            [pAlert addButtonWithTitle:@"确定"];
            [pAlert setAlertStyle:NSWarningAlertStyle];
            [pAlert beginSheetModalForWindow:[self window] completionHandler:^(NSModalResponse returnCode) {
                
            }];
            
        }
    }
    
    if (sender == pBtnCancle)
    {
        NSAlert *pAlert = [[NSAlert alloc] init];
        [pAlert setMessageText:@"警告"];
        [pAlert setInformativeText:@"您还未导入配置文件"];
        [pAlert addButtonWithTitle:@"确定"]; //添加按钮
        [pAlert setAlertStyle:NSWarningAlertStyle]; //设置弹出样式
        [pAlert beginSheetModalForWindow:[self window] completionHandler:^(NSModalResponse returnCode) {
            // 这里的返回值对应按钮的id
        }];
        // 以模态对话框的形式弹出对话框
        //        [pAlert addButtonWithTitle:@"取消"];
        //        long ret = [pAlert runModal];
        //        if(ret == 1000)
        //        {
        //
        //        }
        //        else if(ret == 1001)
        //        {
        //
        //        }
        
    }
}


- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
    return YES; //返回TRUE退出程序
}


- (void)applicationWillTerminate:(NSNotification *)aNotification
{
    // Insert code here to tear down your application 终止程序
    [NSApp terminate:self];
    
}

@end